package com.enqualcomm.kids.mvp.pedometer;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.activities.RPedometerActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.QueryStepDayListParams;
import com.enqualcomm.kids.network.socket.request.QueryStepHourListParams;
import com.enqualcomm.kids.network.socket.response.QueryStepDayListResult;
import com.enqualcomm.kids.network.socket.response.QueryStepHourListResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by yinzhiqun on 2015/10/16.
 *
 */
public class PedometerModel extends NetworkModel {
//     RPedometerActivity pedometerActivity;
     TerminalDefault terminalDefault;
     AppDefault appDefault;
    int threadCount;
    QueryStepHourListResult hourlist;
    QueryStepDayListResult daylist;
    Timer timer;

    public PedometerModel(String terminalid) {
        terminalDefault = new TerminalDefault(terminalid);
        appDefault= new AppDefault();
    }

//    public void attachView(RPedometerActivity pedometerActivity){
//        this.pedometerActivity = pedometerActivity;
//    }

    @Override
    public void onStop() {
        if (timer!=null){
            timer.cancel();
        }
        super.onStop();
    }

//    public void detachView(){
//        pedometerActivity = null;
//    }

    public void start() {
        if (timer!=null){
            timer.cancel();
            timer = null;
        }
        timer = new Timer("refresh");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                refresh();
            }
        },0,60000);
    }

    private void refresh() {
        threadCount = 2;
        String begintime = null;
        String endtime = null;
        // "2015-01-20 00:00:00"

        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        StringBuilder builder = new StringBuilder();
        begintime = builder.append(dateStr).append(" 00:00:00").toString();
        builder.setLength(0);
        endtime = builder.append(dateStr).append(" 23:59:59").toString();

        QueryStepHourListParams params = new QueryStepHourListParams(appDefault.getUserkey(), terminalDefault.getTerminalid(), begintime, endtime);
        loadDataFromServer(new SocketRequest<QueryStepHourListResult>(params, new NetworkListener<QueryStepHourListResult>() {
            @Override
            public void onError(VolleyError error) {
                threadCount--;
                callBack();
            }

            @Override
            public void onSuccess(QueryStepHourListResult response) {
                if (response.code ==0){
                    hourlist = response;
                    terminalDefault.setStepHourList(response.result);
                }
                threadCount--;
                callBack();
            }
        }));

        String dateStr2 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
                System.currentTimeMillis() - 3600l * 1000 * 24 * 6));
        builder.setLength(0);
        begintime = builder.append(dateStr2).append(" 00:00:00").toString();
        QueryStepDayListParams params2 = new QueryStepDayListParams(appDefault.getUserkey(), terminalDefault.getTerminalid(), begintime, endtime);
        loadDataFromServer(new SocketRequest<QueryStepDayListResult>(params2, new NetworkListener<QueryStepDayListResult>() {
            @Override
            public void onError(VolleyError error) {
                threadCount--;
                callBack();
            }

            @Override
            public void onSuccess(QueryStepDayListResult response) {
                if (response.code==0){
                    daylist = response;
                    terminalDefault.setStepDayList(response.result);
                }
                threadCount--;
                callBack();
            }
        }));
    }

    private void callBack() {
        if (threadCount==0&&hourlist!=null&&daylist!=null){
//            pedometerActivity.refresh(hourlist,daylist);
        }
    }


    private void newRefresh() {
        String begintime = null;
        String endtime = null;
        // "2015-01-20 00:00:00"
        StringBuilder builder = new StringBuilder();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        String dateStr2 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
                System.currentTimeMillis() - 3600l * 1000 * 24 * 6));
        begintime = builder.append(dateStr2).append(" 00:00:00").toString();
        builder.setLength(0);
        endtime = builder.append(dateStr).append(" 23:59:59").toString();
        QueryStepDayListParams params2 = new QueryStepDayListParams(appDefault.getUserkey(), terminalDefault.getTerminalid(), begintime, endtime);
        loadDataFromServer(new SocketRequest<QueryStepDayListResult>(params2, new NetworkListener<QueryStepDayListResult>() {
            @Override
            public void onError(VolleyError error) {
            }

            @Override
            public void onSuccess(QueryStepDayListResult response) {
                if (response.code==0){
                    daylist = response;
                    terminalDefault.setStepDayList(response.result);
                }
            }
        }));
    }


}
